home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 2510.ZIP / TRSOURCE.EXE / FILEWRIT.C < prev    next >
C/C++ Source or Header  |  1990-10-22  |  2KB  |  67 lines

  1. /*********
  2. *  FILEWRIT.C
  3. *  by Tom Rettig, Leonard Zerman
  4. * Placed in the public domain by Tom Rettig Associates, 10/22/1990.
  5. *
  6. *  Syntax: FILEWRITE( [<drive>:][\<path>]<filename.ext>, <expC> )
  7. *  Return: <expC> "DONE" if written successfully, otherwise error message.
  8. *********/
  9.  
  10. #include "trlib.h"
  11.  
  12. TRTYPE filewrite()
  13. {
  14.    static char funcname[] = { "filewrite" };
  15.    static char eofmark[] = { (char)EOF_MARK, NULLC };
  16.    char *filename, *instr, *tempstr;
  17.    int filenum, writebytes;
  18.    int i, status;
  19.    long maxbytes;
  20.  
  21.    if ( PCOUNT==2 && ISCHAR(1) && ISCHAR(2) )
  22.    {
  23.       filename = _parc(1);
  24.       instr    = _parc(2);
  25.       filenum  = _tr_creat( filename, FL_NORMAL );
  26.  
  27.       if ( filenum != FILEERROR && *filename && *instr )
  28.       {
  29.          i = 0;
  30.          maxbytes = (long) _tr_strlen(instr);
  31.  
  32.          tempstr = instr;
  33.          while (maxbytes > 32767L)
  34.          {
  35.             if (status = _tr_write( filenum, tempstr, 32767) == -1)
  36.             {
  37.                _retc( _tr_errmsgs(funcname,E_FWRITE) );
  38.                _tr_close( filenum );
  39.             }
  40.             tempstr += 32767;
  41.             maxbytes -= 32767L;
  42.          }
  43.  
  44.          writebytes = _tr_write( filenum, tempstr, (int) maxbytes );
  45.  
  46.          if ( writebytes != FILEERROR )
  47.          {
  48.             _tr_write( filenum, eofmark, 1 );
  49.             if ( !_tr_close(filenum) )      /* _tr_close returns 0 if successful */
  50.                _retc( DONE );
  51.             else
  52.                _retc( _tr_errmsgs(funcname,E_FCLOSE) );
  53.          }
  54.          else
  55.          {
  56.             _retc( _tr_errmsgs(funcname,E_FWRITE) );
  57.             _tr_close( filenum );
  58.          }
  59.       }
  60.       else
  61.          _retc( _tr_errmsgs(funcname,E_FOPEN) );
  62.    }
  63.    else
  64.       _retc( _tr_errmsgs(funcname,E_SYNTAX) );
  65. }
  66.  
  67.